/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.controller.cp;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import vn.webapp.controller.BaseWeb;
import vn.webapp.dto.DataPage;
import vn.webapp.form.SessionValidationForm;
import vn.webapp.model.Sessions;
import vn.webapp.service.SessionsService;
/**
 *
 * @author Tonytran
 */
@Controller("cpSessions")
@RequestMapping(value = {"/cp"})
public class SessionsController extends BaseWeb{
    
    @Autowired
    private SessionsService sessionsService;
    
    private int nav = 6;
    /**
     * 
     * @param model
     * @return 
     */
    @RequestMapping(value = "/sessions" , method = RequestMethod.GET)
    public String classes(ModelMap model, HttpSession session){
        DataPage<Sessions> dataSessions = sessionsService.list();
        model.put("sz_Response", session.getAttribute("response"));
        session.setAttribute("response", "");
        model.put("dataSessions", dataSessions);
        model.put("nav", this.nav);
        return "cp.sessions";
    }
    
    /**
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/sessiondetail/{id}")
    public String viewDetail(ModelMap model, @PathVariable("id") String id) {
        Sessions session = sessionsService.viewDetail(id);
        model.put("sessionForm", new SessionValidationForm());
        model.put("dataSession", session);
        model.put("nav", this.nav);
        return "cp.sessiondetail";
    }
    

    @RequestMapping(value = "/savesessiondetail", method = RequestMethod.POST)
    public String processEditClass(@Valid @ModelAttribute("sessionForm") SessionValidationForm sessionForm,
            BindingResult result,HttpServletRequest request, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
           return "cp.sessiondetail";
        }else
        {
            String SessionYear = sessionForm.getYear();
            int SessionSemester = sessionForm.getSemester();
            int SessionId = sessionForm.getId();
            Sessions o_Session = sessionsService.checkExistsYear(SessionYear, SessionSemester, SessionId);
            if(o_Session != null)
            {
                model.put("sz_Response", "The name "+SessionYear+" is being used.");
                return "cp.sessiondetail";
            }else{
                int i_UpdateSession = sessionsService.updateTheSession(SessionYear, SessionSemester, SessionId);
                session.setAttribute("response", "Edit successfully!");
                return "redirect:" + this.baseUrl + "/cp/sessions.html";
            }
        }
    }
    
    @RequestMapping(value = "/removesession/{id}", method = RequestMethod.GET)
    public String processRemoveSession(ModelMap model, @PathVariable("id") int id, HttpSession session) {
        int i_IsRemoved = sessionsService.removeSession(id);
        String sz_Response;
        if (i_IsRemoved != 0) {
            sz_Response = "Remove the session successfully !";
        } else {
            sz_Response = "Remove the session not successfully. Please retry";
        }
        session.setAttribute("response", sz_Response);
        return "redirect:" + this.baseUrl + "/cp/sessions.html";
    }
    
    @RequestMapping(value = "/addnewsession", method = RequestMethod.GET)
    public String addnewsession(Map model){
        model.put("nav", this.nav);
        model.put("sessionFormAdd", new SessionValidationForm());
        return "cp.addnewsession";
    }
    
    @RequestMapping(value = "/savesession", method = RequestMethod.POST)
    public String processAddNewSession(@Valid @ModelAttribute("sessionFormAdd") SessionValidationForm sessionFormAdd,
            BindingResult result, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
           return "cp.addnewsession";
        }else
        {
            String SessionYear = sessionFormAdd.getYear();
            int SessionSemester = sessionFormAdd.getSemester();
            int SessionId = sessionFormAdd.getId();
            Sessions o_Session = sessionsService.checkExistsYear(SessionYear, SessionSemester, SessionId);
            if(o_Session != null)
            {
                model.put("sz_Response", "The name "+SessionYear+" is existed.");
                return "cp.addnewsession";
            }else
            {
                int i_InsertASession = sessionsService.saveASession(SessionYear, SessionSemester);
                session.setAttribute("response", "Add successfully!");
                return "redirect:" + this.baseUrl + "/cp/sessions.html";
            }
        }
    }
}
